//
//  PixelView.m
//  LeaderMicroLesson
//
//  Created by 鲁曦广 on 16/1/19.
//  Copyright © 2016年 WuhanBttenMobileTechnologyCo.,Ltd. All rights reserved.
//

#import "PixelView.h"
#define SINGLE_LINE_WIDTH           (1 / [UIScreen mainScreen].scale)
#define SINGLE_LINE_ADJUST_OFFSET   ((1 / [UIScreen mainScreen].scale) / 2)

@implementation PixelView
@synthesize gridColor = _gridColor;
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
   self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        _gridColor = [UIColor blackColor];
        _gridLineWidth = SINGLE_LINE_WIDTH;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        _gridColor = [UIColor blackColor];
        _gridLineWidth = SINGLE_LINE_WIDTH;
    }
    
    return self;
}

- (void)setGridColor:(UIColor *)gridColor
{
    _gridColor = gridColor;
    
    [self setNeedsDisplay];
}

- (void)setGridLineWidth:(CGFloat)gridLineWidth
{
    _gridLineWidth = gridLineWidth;
    
    [self setNeedsDisplay];
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(context);
    if (((int)(self.gridLineWidth * [UIScreen mainScreen].scale) + 1) % 2 == 0) {
        self.gridLineWidth = SINGLE_LINE_ADJUST_OFFSET;
    }
    CGContextMoveToPoint(context, self.bounds.origin.x, 0);
    CGContextAddLineToPoint(context, self.bounds.size.width, 0);
    CGContextSetLineWidth(context, self.gridLineWidth);
    CGContextSetStrokeColorWithColor(context, self.gridColor.CGColor);
    CGContextStrokePath(context);
}



@end
